import * as officegen from 'officegen'
import fs = require('fs')
import path = require('path')
import CodeReader from './CodeReader'

const configs = [
    {
        fromDir: `/Users/lyaotian/Documents/Work/MyProject/BookTravel/AndroidBookTravel/app`,
        toFile: `booktravel_android.docx`,
        checkAvailable: (name: string) => {
            if (
                name.endsWith(".kt") ||
                name.endsWith(".java") ||
                name.endsWith(".xml")
            ) {
                return true
            }
            return false
        }
    },
    {
        fromDir: `/Users/lyaotian/Documents/Work/MyProject/BookTravel/iBookTravel/iBookTravel`,
        toFile: `booktravel_ios.docx`,
        checkAvailable: (name: string) => {
            if (
                name.endsWith(".swift") ||
                name.endsWith(".h") ||
                name.endsWith(".m")
            ) {
                return true
            }
            return false
        }
    },
]

configs.forEach(config => {
    // docx
    const dataDir = path.resolve(__dirname, '../data')
    const outFile = fs.createWriteStream(`${dataDir}/${config.toFile}`)

    const docx = officegen({
        type: 'docx',
        orientation: 'portrait',
        // pageMargins: { top: 1000, left: 1000, bottom: 1000, right: 1000 },
    })

    docx.on('error', (err: Error) => {
        console.error(err)
    })

    const callback = (line: string) => {
        var pObj = docx.createP()
        pObj.addText(`${line}`)
    }
    const reader = new CodeReader(
        config.fromDir,
        callback,
        config.checkAvailable
    )

    outFile.on('close', () => {
        console.log('Finish to create a DOCX file.')
    })
    docx.generate(outFile)
})
